package com.sky.controller.admin;

import com.sky.annotation.LimitAccess;
import com.sky.annotation.PreAuthorize;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO dto
     * @return R
     */
    @GetMapping("/page")
    public Result<PageBean> pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageBean pageBean = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO dto
     * @return R
     */
    @PostMapping
    @PreAuthorize("admin:dish:addDishWithFlavor")
    public Result<String> addDishWithFlavor(@RequestBody DishDTO dishDTO) {
        dishService.addDishWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     *
     * @param ids 菜品id集合
     * @return R
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    @PreAuthorize("admin:dish:deleteByIds")
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除菜品:{}", ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品详情
     *
     * @param id 菜品id
     * @return R
     */
    @ApiOperation("根据id查询菜品详情")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品详情:{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO dishdto
     * @return R
     */
    @LimitAccess()
    @PreAuthorize("admin:dish:update")
    @ApiOperation("修改菜品信息")
    @PutMapping
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品信息:{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售、停售
     *
     * @param status 菜品状态
     * @param id     菜品id
     * @return R
     */
    @LimitAccess
    @PostMapping("/status/{status}")
    @PreAuthorize("admin:dish:updateStatus")
    public Result<String> updateStatus(@PathVariable Integer status, Long id) {
        dishService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId 分类id
     * @return R
     */
    @GetMapping("/list")
    public Result<List<Dish>> selectByCategoryId(Long categoryId) {
        List<Dish> dishes = dishService.selectByCategoryId(categoryId);
        return Result.success(dishes);
    }
}
